package cn.droidlover.xdroidmvp.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.List;

import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.NToast;
import cn.droidlover.xdroidmvp.mvp.IPresent;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xdroidmvp.view.DividerItemDecoration;

/**
 * Created by gwy on 2017/11/21.
 */

public abstract class BaseLazyListFragment<T, P extends IPresent> extends XLazyFragment<P> implements ListViewItemClickListener, ListViewChildClickListener {

    protected int mStartIndex = 1;

    protected boolean isRefresh;
    protected RecyclerView mRecyclerview;
    protected TwinklingRefreshLayout mRefreshLayout;
    protected BaseRecyclerAdapter<T> mBaseAdapter;


    @Override
    public int getContentViewId() {
        return R.layout.activity_list;
    }

    @Override
    public void initView() {
        initRecyclerView(rootView);
        setAdapter();
        initRefreshLayout(rootView);
        mainView = mRecyclerview;
        super.initView();
    }

    public void initRecyclerView(View mainView) {
        mRecyclerview = mainView.findViewById(R.id.recyclerview);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        DividerItemDecoration dec = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
//        mRecyclerview.addItemDecoration(dec);

        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        mRecyclerview.setLayoutManager(layoutManager);
        mRecyclerview.setHasFixedSize(true);
        mRecyclerview.setNestedScrollingEnabled(false);
    }

    private void setAdapter() {
        mBaseAdapter = getAdapter();
        mRecyclerview.setAdapter(mBaseAdapter);
        mBaseAdapter.setItemClickListener(this);
        mBaseAdapter.setChildClickListener(this);
    }

    @Override
    public void onFail(String message) {
        super.onFail(message);
        twinkCompleted();
    }

    public void onSuccess(List<T> list) {
        mBaseAdapter.addList(isRefresh, list);
        if (!Kits.Empty.check(list)) {
            mStartIndex++;
        }
        if (0 == mBaseAdapter.getmList().size()) {
            showNoDataPage();
        } else {
            showMainView();
        }
        twinkCompleted();
    }

    public void onReLoadSuccess(List<T> list) {
        mBaseAdapter.addList(true, list);
        if (!Kits.Empty.check(list)) {
            mStartIndex++;
        }
        if (0 == mBaseAdapter.getmList().size()) {
            showNoDataPage();
        } else {
            showMainView();
        }
        twinkCompleted();
    }

    public void onSuccess(T t) {
        mBaseAdapter.add(t);
        if (0 == mBaseAdapter.getmList().size()) {
            showNoDataPage();
        } else {
            showMainView();
        }
        twinkCompleted();
    }

    public void hadNoData(){
        NToast.shortToast("没有更多数据了");
        twinkCompleted();
        mRefreshLayout.setEnableLoadmore(false);
    }

    @Override
    public boolean showNoDataPage() {
        if (0 == mBaseAdapter.getmList().size() || isRefresh) {
            return super.showNoDataPage();
        } else {
            showMainView();
        }
        return false;
    }

    @Override
    public void twinkCompleted() {
        if (mRefreshLayout != null) {
            if (isRefresh) {
                mRefreshLayout.finishRefreshing();
            } else {
                mRefreshLayout.finishLoadmore();
            }
        }
    }


    public void initRefreshLayout(View mainView) {
        mRefreshLayout = mainView.findViewById(R.id.refresh);
        if (mRefreshLayout == null) {
            return;
        }
        SinaRefreshView headerView = new SinaRefreshView(getActivity());
        mRefreshLayout.setHeaderView(headerView);
        mRefreshLayout.setMaxHeadHeight(140);
        mRefreshLayout.setOverScrollBottomShow(false);
        mRefreshLayout.setAutoLoadMore(true);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                isRefresh = true;
                mRefreshLayout.setAutoLoadMore(true);
                getListDatas(true);
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                isRefresh = false;
                getListDatas(false);
            }
        });
        LoadingView loadingView = new LoadingView(getActivity());
        mRefreshLayout.setBottomView(loadingView);
    }


    public abstract BaseRecyclerAdapter<T> getAdapter();

    public void getListDatas(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            mStartIndex = 1;
        }
    }

    @Override
    public void itemChildClick(int position, int viewId) {

    }

    @Override
    public void getIntentData() {

    }

    @Override
    public void loadFail() {
        twinkCompleted();
    }
}
